layui.use(['layer', 'form', 'table', 'admin',  'laydate','ax',"upload","laypage"], function () {
    //var $ = layui.$;
    var $= layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;
    var laypage = layui.laypage;
    /**
     * 系统管理--评价管理
     */
    var OBJ = {
        tableId: "meterReadingTable",    //表格id
        condition: {
            meterNo: "",
            shopName:""
        }
    };

    /**
     * 初始化表格的列
     */
    OBJ.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'meterNo', sort: true, title: '能源表编号'},
            {field: 'shopName', sort: true, title: '品牌名称'},
            {field: 'shopAdr', sort: true, title: '铺位号'},
            {field: 'meterName', sort: true, title: '能源表类型'},
            {field: 'chargeTypeName', sort: true, title: '收费类型'},
            {field: 'meterModelName', sort: true, title: '能源表模式'},
            {field: 'meterRedingMonth', sort: true, title: '抄表所属期'},
            {field: 'startMum', sort: true, title: '起码'},
            {field: 'endMum', sort: true, title: '止码'},
            {field: 'currentMum', sort: true, title: '本期用量'},
            {field: 'unitPrice', sort: true, title: '单价'},
            {field: 'timesRate', sort: true, title: '倍率'},
            {field: 'discount', sort: true, title: '折扣'},
            {field: 'totlePrice', sort: true, title: '金额'},
            {field: 'floatAdjust', sort: true, title: '浮动调整'},
            {field: 'statusName', sort: true, title: '状态'},
            {field: 'dataSourse', sort: true, title: '数据来源'},
            {align: 'left', toolbar: '#tableBar', title: '操作', minWidth: 200,fixed: 'right'}
        ]];
    };
    //导入 用layui upload插件
    var upload = layui.upload;

    //执行实例
    /*var uploadInst = upload.render({
        elem: '#importData' //绑定元素
        ,url: Feng.ctxPath + '/meterReading/importData' //上传接口
        ,done: function(res){
            //上传完毕回调
            alert(1);
        }
        ,error: function(){
            //请求异常回调
            alert(2);
        }
    });*/
    //执行上传
    var uploadInst = upload.render({
        elem: '#importData' //绑定元素
        ,url: Feng.ctxPath + '/meterReading/importData' //上传接口
        ,method: 'POST'
        ,accept: 'file'
        ,size: 5000
        ,auto: false //选择文件后不自动上传
        ,choose: function(obj){
//                    确定导入文件并进行线上合规验证吗？
            //确认框
            layer.confirm('确定导入文件吗？', {icon: 3, title:'提示'}, function(index){
                //读取本地文件
                obj.preview(function(index, file, result){
                    //单个重传
                    obj.upload(index, file);
                    layer.load();
                });
                layer.close(index);
            });
        }
        ,done: function(res){//上传完毕回调
            if(res.code==1){
                //layer.msg('上传成功,成功'+res.successNum+'条,失败'+res.failNum+'条,为空'+res.nullNum+'条！ \n '+res.msg);
                layer.alert('上传成功,成功'+res.successNum+'条,失败'+res.failNum+'条,为空'+res.nullNum+'条！ <br/>'+res.msg, {
                });
            }else{
                layer.msg('上传失败,'+res.msg);
            }
            layer.closeAll('loading');
            table.reload(OBJ.tableId);

        },error: function(res){
            layer.closeAll('loading');
            layer.msg('上传失败');
        }
    });
    /**
     * 点击查询按钮
     */
    OBJ.search = function () {
        var queryData = {};
        queryData['meterNo'] = $("#meterNo").val().replace(/\s+/g,"");
        queryData['shopName'] = $("#shopName").val().replace(/\s+/g,"");
        queryData['shopAdr'] = $("#shopAdr").val().replace(/\s+/g,"");
        queryData['monthTime'] = $("#monthTime").val();
        queryData['auditStatus'] = $("#auditStatus").val();
        queryData['meterType'] = $("#meterType").val();
        queryData['meterModel'] = $("#meterModel").val();
        debugger
        table.reload(OBJ.tableId,{
            page:{
                curr:1
            },
            where: queryData
        })
    };
    //渲染时间选择框
    //年月选择器
    laydate.render({
        elem: '#monthTime'
        ,type: 'month'
    });

    /**
     * 弹出添加抄表
     */
    OBJ.openAddMeterReading = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '新增抄表',
            content: Feng.ctxPath + '/meterReading/add',
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            }
        });
    };
    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onDeleteMeterReading = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/meterReading/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(OBJ.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("meterReadingId", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除抄表信息 ?", operation);
    };
    /**
     * 点击详情
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.onDetailPayment = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            area: '1000px',
            title: '缴费详情 &nbsp(表编号:'+data.meterNo+')',
            content: Feng.ctxPath + '/payment/payment_detail?id=' + data.id+'&payStatusName='+data.payStatusName,
            end: function () {
                admin.getTempData('formOk') && table.reload(OBJ.tableId);
            },
            btn: ['返回'],
            btnAlign: 'c'
        });
    };


    /**
     * 导出excel按钮
     */
    OBJ.exportExcel = function () {
        var checkRows = table.checkStatus(OBJ.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };


    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OBJ.tableId,
        url: Feng.ctxPath + '/meterReading/list',
        page: {
            limits:[10,20,30,50,100,'ALL']
        },
        height: "full-158",
        title:"抄表信息维护列表",
        cellMinWidth: 100,
        cols: OBJ.initColumn(),
        done:function(res, curr, count){
            if (this.limit ==count){
                $(".layui-laypage-limits select").find("option[value='ALL']").attr("selected",true);
            }
            $(".layui-laypage-limits select").find("option[value='ALL']").attr("value",count).text('全部');
        }

    });

    /**
     * 点击审核
     *
     * @param data 点击按钮时候的行数据
     */
    OBJ.auditData = function () {
        var checkRows = table.checkStatus(OBJ.tableId);
        admin.putTempData('formOk', false);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要审核的数据");
        } else {
            var ids= "";
            for(var i=0;i<checkRows.data.length;i++){
                ids += checkRows.data[i].id+",";
            }
            top.layui.admin.open({
                type: 2,
                area: '1000px',
                title: '抄表审核,总条数:'+checkRows.data.length,
                content: Feng.ctxPath + '/meterReading/audit?ids=' + ids,
                end: function () {
                    admin.getTempData('formOk') && table.reload(OBJ.tableId);
                }
            });
        }
    };
    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OBJ.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        OBJ.openAddMeterReading();
    });
    // 导出excel
    $('#btnExp').click(function () {
        OBJ.exportExcel();
    });
    // 导入excel
    $('#importData').click(function () {
        OBJ.importData();
    });
    $('#auditData').click(function () {
        OBJ.auditData();
    });
    //重置
    $("#reset").click(function () {
        var queryData = {};
        $("#meterNo").val("");
        $("#shopName").val("");
        $("#shopAdr").val("");
        $("#monthTime").val("");
        $("#auditStatus").val("");
        $("#meterType").val("");
        $("#meterModel").val("");
        form.render();
        queryData['meterNo'] = $("#meterNo").val();
        queryData['shopName'] = $("#shopName").val();
        queryData['shopAdr'] = $("#shopAdr").val();
        queryData['monthTime'] = $("#monthTime").val();
        queryData['auditStatus'] = $("#auditStatus").val();
        queryData['meterType'] = $("#meterType").val();
        queryData['meterModel'] = $("#meterModel").val();
        table.reload(OBJ.tableId,{
            where: queryData
        })
        table.reload(OBJ.tableId);

    })

    // 工具条点击事件
    table.on('tool(' + OBJ.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'dispatch') {
            OBJ.onDispatchTicket(data);
        } else if (layEvent === 'delete') {
            OBJ.onDeleteMeterReading(data);
        } else if (layEvent === 'detail') {
            OBJ.onDetailPayment(data);
        }
    });
});
